package com.buanalintas.sandals.pos.itemstock;

import com.buanalintas.bynga.Controller;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Utility;
import com.buanalintas.bynga.entities.MasterUnit;
import com.buanalintas.bynga.entities.MasterUser;
import com.buanalintas.sandals.pos.entities.PosTransactionItemStock;
import java.util.List;
import javax.servlet.http.HttpSession;

public class ItemStock extends Page {

    private Field itemStockId;
    private Field itemStockUserId;
    private Field itemStockUnitId;
    private Field itemStockItemId;
    private Field itemStockStatusId;
    private Field itemStockSizeId;
    private Field itemStockType;
    private Field itemStockLast;
    private Field itemStockValue;
    private Field itemStockCurrent;
    private Field itemStockTimestamp;

    public ItemStock() {

        setTheClass(PosTransactionItemStock.class);

        itemStockId = new Field();
        itemStockId.setName("itemStockId");
        itemStockId.setCaption("id");
        itemStockId.setListProperties("");
        itemStockId.setInputProperties("");
        getFields().add(itemStockId);

        itemStockUserId = new Field();
        itemStockUserId.setName("itemStockUserId");
        itemStockUserId.setCaption("user");
        itemStockUserId.setListProperties(" align=\"left\" size=\"10\"");
        itemStockUserId.setInputProperties("");
        getFields().add(itemStockUserId);

        itemStockUnitId = new Field();
        itemStockUnitId.setName("itemStockUnitId");
        itemStockUnitId.setCaption("unit");
        itemStockUnitId.setListProperties(" align=\"left\" size=\"10\"");
        itemStockUnitId.setInputProperties("");
        getFields().add(itemStockUnitId);

        itemStockItemId = new Field();
        itemStockItemId.setName("itemStockItemId");
        itemStockItemId.setCaption("item");
        itemStockItemId.setListProperties(" align=\"left\" size=\"20\"");
        itemStockItemId.setInputProperties("");
        getFields().add(itemStockItemId);

        itemStockStatusId = new Field();
        itemStockStatusId.setName("itemStockStatusId");
        itemStockStatusId.setCaption("status");
        itemStockStatusId.setListProperties(" align=\"center\" size=\"5\"");
        itemStockStatusId.setInputProperties(" ");
        itemStockStatusId.setInputType(Field.INPUT_COMBO_BOX);
        getFields().add(itemStockStatusId);
        
        itemStockSizeId = new Field();
        itemStockSizeId.setName("itemStockSizeId");
        itemStockSizeId.setCaption("size");
        itemStockSizeId.setListProperties(" align=\"center\" size=\"5\"");
        itemStockSizeId.setInputProperties(" ");
        itemStockSizeId.setInputType(Field.INPUT_CHECK_BOX);
        getFields().add(itemStockSizeId);

        itemStockType = new Field();
        itemStockType.setName("itemStockType");
        itemStockType.setCaption("type");
        itemStockType.setListProperties(" align=\"left\" size=\"10\" ");
        itemStockType.setInputProperties(" ");
        itemStockType.getOptions().add(new String[]{"1", "Sale"});
        itemStockType.getOptions().add(new String[]{"2", " Receipt"});
        itemStockType.getOptions().add(new String[]{"3", "Distribution"});
        itemStockType.getOptions().add(new String[]{"4", "Retur Supplier"});
        itemStockType.getOptions().add(new String[]{"5", "Retur Shop"});
        itemStockType.getOptions().add(new String[]{"6", "Sale Shop"});
        itemStockType.getOptions().add(new String[]{"7", "Opname"});
        itemStockType.getOptions().add(new String[]{"8", " Sale By Request"});
        itemStockType.getOptions().add(new String[]{"9", " Distribution By Request"});
        itemStockType.setInputType(Field.INPUT_COMBO_BOX);
        getFields().add(itemStockType);

        itemStockLast = new Field();
        itemStockLast.setName("itemStockLast");
        itemStockLast.setCaption("stock last");
        itemStockLast.setListProperties(" align=\"right\" size=\"5\"");
        itemStockLast.setInputProperties(" ");
        getFields().add(itemStockLast);

        itemStockValue = new Field();
        itemStockValue.setName("itemStockValue");
        itemStockValue.setCaption("stock value");
        itemStockValue.setListProperties(" align=\"right\" size=\"5\"");
        itemStockValue.setInputProperties(" ");
        getFields().add(itemStockValue);

        itemStockCurrent = new Field();
        itemStockCurrent.setName("itemStockCurrent");
        itemStockCurrent.setCaption("stock current");
        itemStockCurrent.setListProperties(" align=\"right\" size=\"5\"");
        itemStockCurrent.setInputProperties(" ");
        getFields().add(itemStockCurrent);

        itemStockTimestamp = new Field();
        itemStockTimestamp.setName("itemStockTimestamp");
        itemStockTimestamp.setCaption("timestamp");
        itemStockTimestamp.setListProperties(" align=\"center\" size=\"10\"");
        itemStockTimestamp.setInputProperties(" ");
        getFields().add(itemStockTimestamp);

        fillFields();

        getFieldsAdd().clear();
        getFieldsAdd().add(itemStockStatusId);

        getFieldsEdit().clear();
        getFieldsEdit().add(itemStockItemId);
        getFieldsEdit().add(itemStockStatusId);

        getFieldsList().clear();
        getFieldsList().add(itemStockUserId);
        getFieldsList().add(itemStockUnitId);
        getFieldsList().add(itemStockSizeId);
        getFieldsList().add(itemStockItemId);
        getFieldsList().add(itemStockStatusId);
        getFieldsList().add(itemStockType);
        getFieldsList().add(itemStockLast);
        getFieldsList().add(itemStockValue);
        getFieldsList().add(itemStockCurrent);
        getFieldsList().add(itemStockTimestamp);

        getFieldsView().clear();
        getFieldsView().add(itemStockUserId);
        getFieldsView().add(itemStockUnitId);
        getFieldsView().add(itemStockItemId);
        getFieldsList().add(itemStockSizeId);
        getFieldsView().add(itemStockStatusId);
        getFieldsView().add(itemStockType);
        getFieldsView().add(itemStockLast);
        getFieldsView().add(itemStockValue);
        getFieldsView().add(itemStockCurrent);
        getFieldsView().add(itemStockTimestamp);

        setExportList(getClass().getResourceAsStream("ItemStockList.jasper"));
        setExport(getClass().getResourceAsStream("ItemStock.jasper"));
        setController(new ItemStockController(this));
    }

    class ItemStockController extends Controller {

        public ItemStockController(Page page) {
            super(page);
            setOrder(itemStockTimestamp);
            setOrderType("DESC");
        }

        @Override
        public void afterInsert(Object data, List list) {
            PosTransactionItemStock itemStock = (PosTransactionItemStock) list;
            itemStock.setItemStockUserId(getCurrentUser());
            itemStock.setItemStockUnitId(getCurrentUnit());
            itemStock.setItemStockTimestamp(Utility.getTimestamp());
        }
    }

    @Override
    public void beforeExport() {
        getExportParams().put("title", Dictionary.getValue(this, "Item Stock"));
    }

    @Override
    public void beforeExportMulti() {
        beforeExport();
    }
}
